/*
 * Created on 2005-1-24
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.nauu.atombuilder.parts;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.CompoundSnapToHelper;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.SnapToGeometry;
import org.eclipse.gef.SnapToGrid;
import org.eclipse.gef.SnapToGuides;
import org.eclipse.gef.SnapToHelper;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.editpolicies.SnapFeedbackPolicy;
import org.eclipse.gef.rulers.RulerProvider;

import com.nauu.atombuilder.modle.Diagram;
import com.nauu.atombuilder.policies.ConnectionBendpointEditPolicy;
import com.nauu.atombuilder.policies.FormDiagramLayoutEditPolicy;

/**
 * @author nauu
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class FormDiagramPart extends AbstractGraphicalEditPart implements PropertyChangeListener {

    protected List getModelChildren() {
        return ((Diagram) this.getModel()).getNodes();
    }

    public void activate() {
        super.activate();
        ((Diagram) getModel()).addPropertyChangeListener(this);
    }

    public void deactivate() {
        super.deactivate();
        ((Diagram) getModel()).removePropertyChangeListener(this);
    }

    
    @Override
   	public Object getAdapter(Class adapter) {
   		if (adapter == SnapToHelper.class) {
   			List snapStrategies = new ArrayList();
   			Boolean val = (Boolean) getViewer().getProperty(
   					RulerProvider.PROPERTY_RULER_VISIBILITY);
   			if (val != null && val.booleanValue())
   				snapStrategies.add(new SnapToGuides(this));
   			val = (Boolean) getViewer().getProperty(
   					SnapToGeometry.PROPERTY_SNAP_ENABLED);
   			if (val != null && val.booleanValue())
   				snapStrategies.add(new SnapToGeometry(this));
   			val = (Boolean) getViewer().getProperty(
   					SnapToGrid.PROPERTY_GRID_ENABLED);
   			if (val != null && val.booleanValue())
   				snapStrategies.add(new SnapToGrid(this));

   			if (snapStrategies.size() == 0)
   				return null;
   			if (snapStrategies.size() == 1)
   				return snapStrategies.get(0);

   			SnapToHelper ss[] = new SnapToHelper[snapStrategies.size()];
   			for (int i = 0; i < snapStrategies.size(); i++)
   				ss[i] = (SnapToHelper) snapStrategies.get(i);
   			return new CompoundSnapToHelper(ss);
   		}
   		return super.getAdapter(adapter);
   	}
    
    public void propertyChange(PropertyChangeEvent evt) {
        String prop = evt.getPropertyName();
        if (Diagram.PROP_NODE.equals(prop)){
        	refreshChildren();
        }
    }

    protected IFigure createFigure() {
        Figure figure = new FreeformLayer();
        figure.setLayoutManager(new FreeformLayout());
        return figure;
    }

    protected void createEditPolicies() {
        installEditPolicy(EditPolicy.LAYOUT_ROLE, new FormDiagramLayoutEditPolicy());
        installEditPolicy("Snap Feedback", new SnapFeedbackPolicy());
//        installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE,new ContainerHighlightEditPolicy());
    }

}